import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    # 子弹类

    def __init__(self, ai_game):
        super().__init__() # super()是一种特殊方法，调用父类的方法。此处super().__init__()即调用父类的初始化方法

        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

        # 创建一个表示子弹的矩形
        self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height) # (left: float, top: float, width: float, height: float)
        self.rect.midtop = ai_game.ship.rect.midtop # 子弹初始位置是飞船的最高点

        # 用小数表示的子弹位置
        self.y = float(self.rect.y)

    def update(self):
        self.y -= self.settings.bullet_speed # 更新子弹坐标，子弹只要出现，就会不停向上运动
        self.rect.y = self.y # 将子弹的坐标写入实际显示矩形的坐标

    def draw_bullet(self):
        pygame.draw.rect(self.screen, self.color, self.rect) # draw.rect绘制矩形
        # (surface: Surface, color: _ColorValue, rect: _RectValue, width: int = 0, border_radius: int = -1, border_top_left_radius: int = -1, border_top_right_radius: int = -1, border_bottom_left_radius: int = -1, border_bottom_right_radius: int = -1) -> Rect
